<template>
  <el-menu default-active="active" class="el-menu-vertical-demo" :collapse="isCollapse" background-color="#545c64"
    text-color="#fff" active-text-color="#ffd04b" @select="menuClick">
    <h3 ref="headThree">通用后台管理系统</h3>
    <!-- 上面三个 -->
    <el-menu-item v-for="item in noChildren" :key="item.name" :index="item.name" @click="clickMenu(item)">
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <!-- 其他 -->
    <el-submenu v-for="item in isChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="itemChildrenArr in item.children" :key="itemChildrenArr.path">
        <el-menu-item :index="itemChildrenArr.path" @click="clickMenu(itemChildrenArr)">{{ itemChildrenArr.label
        }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      menuData: [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home",
          url: "Home/Home",
        },
        {
          path: "/mall",
          name: "mall",
          label: "商品管理",
          icon: "video-play",
          url: "MallManage/MallManage",
        },
        {
          path: "/user",
          name: "user",
          label: "用户管理",
          icon: "user",
          url: "UserManage/UserManage",
        },
        {
          label: "其他",
          icon: "location",
          children: [
            {
              path: "/page1",
              name: "page1",
              label: "页面1",
              icon: "setting",
              url: "Other/PageOne",
            },
            {
              path: "/page2",
              name: "page2",
              label: "页面2",
              icon: "setting",
              url: "Other/PageTwo",
            },
          ],
        },
      ],
      // headThreeWidth: this.$refs.headThree.offsetWidth
    };
  },

  methods: {
    clickMenu(item) {
      if (
        this.$route.path !== item.path && !(this.$route.path === "/home" && item.path === "/")
      ) {
        // 当前在/home并且用户点的是/,两者地址去向一致，取反后为false,不会进行跳转
        this.$router.push(item.path);
        this.$store.commit("modifyBreadcrumbData", item);
      }
    },
    // 通过路由使侧边栏文字高亮
    // this.$router.options.routes[0].children 获取到定义的路由配置项
    routerAddActive() {
      const current = this.$route.path.slice(1)
      const routes = this.$router.options.routes[0].children
      let index = -1;
      for (let i = 0; i < routes.length; i++) if (current === routes[i].path) index = i
      return index
    },
    menuClick(index) {
      this.$router.push(index)
    },
  },
  computed: {
    isChildren() {
      return this.menuData.filter((item) => item.children);
    },
    noChildren() {
      return this.menuData.filter((item) => !item.children);
    },
    // vuex控制aside状态
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
  },
  // watch:{
  //   headThreeWidth:{
  //     handler(val){
  //       if(val < 200) this.$refs.headThree.style.display = 'none'
  //     }
  //   }
  // }
};
</script>


<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  height: 100%;
  height: 100%;
  h3 {
    text-align: center;
    color: #fff;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

